// src/services/BaseService.js
import { Toast } from "antd-mobile";
import http from "@/utils/request";
import {
  getTokenCache
} from "./storage";

class BaseService {
  async request(url, method = 'POST', data = {}, requireAuth = false) {
    try {
      console.log("url: " + url);
      // 需要认证的请求检查token
      if (requireAuth) {
        const token = getTokenCache();
        if (!token) {
          Toast.show("请先登录!");
          return { success: false, message: "未授权" };
        }
        data = { ...data, token };
      }

      // 发送请求
      const response = await http.post(url, data);

      // 处理响应
      const resCode = parseInt(response.code);
      if (resCode !== 0) {
        Toast.show(response.msg || "请求失败");
        return { success: false, message: response.msg, data: response.data };
      }

      return { success: true, message: response.msg, data: response.data };
    } catch (error) {
      console.error("请求错误:", error);
      Toast.show("网络请求失败");
      return { success: false, message: "网络请求失败" };
    }
  }
}

export default BaseService;